package com.net.oil.gaode.adpater;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.amap.api.navi.model.AMapNaviPath;
import com.net.oil.R;
import com.net.oil.gaode.util.AMapUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：lch
 * 时间：2018/4/14 0014:上午 11:58
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class RoutRecyclerViewAdapter extends RecyclerView.Adapter<RoutRecyclerViewAdapter.ViewHolder> {


    private Context context;
    private LayoutInflater inflater;

    private List<AMapNaviPath> list;

    private OnItemClickListener onItemClickListener;

    private int selectPosition = 0;

    public RoutRecyclerViewAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        list = new ArrayList<>();
    }

    public void updateData(List<AMapNaviPath> list){
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.route_plan_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final AMapNaviPath path = list.get(position);
        final ViewHolder viewHolder = holder;
        if (path != null) {
            if (path.getLabels() != null){
                holder.routeLabel.setText(path.getLabels());
            }
            holder.needTime.setText(AMapUtil.getFriendlyTime(path.getAllTime()));

            holder.routeLength.setText(AMapUtil.getFriendlyLength(path.getAllLength()));

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onClick(position);
                    changeTextColor(viewHolder, position);
                    selectPosition = position;
                    notifyDataSetChanged();
                }
            });

            changeTextColor(holder, position);

        }
    }

    private void changeTextColor(ViewHolder holder, int position){
        if (position == selectPosition){
            holder.routeLength.setTextColor(context.getResources().getColor(R.color.blue));
            holder.needTime.setTextColor(context.getResources().getColor(R.color.blue));
            holder.routeLabel.setTextColor(context.getResources().getColor(R.color.blue));
        }else {
            holder.routeLength.setTextColor(context.getResources().getColor(R.color.black));
            holder.needTime.setTextColor(context.getResources().getColor(R.color.black));
            holder.routeLabel.setTextColor(context.getResources().getColor(R.color.black));
        }
    }

    @Override
    public int getItemCount() {
        return list.size() > 3 ? 3 : list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.route_label)
        TextView routeLabel;
        @BindView(R.id.need_time)
        TextView needTime;
        @BindView(R.id.route_length)
        TextView routeLength;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener{
        void onClick(int position);
    }
}
